package com.miqtech.master.wyrecorder.ui;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.miqtech.master.wyrecorder.R;
import com.miqtech.master.wyrecorder.annotations.LayoutId;
import com.miqtech.master.wyrecorder.base.BaseAppCompatActivity;
import com.miqtech.master.wyrecorder.constants.API;
import com.miqtech.master.wyrecorder.entity.User;
import com.miqtech.master.wyrecorder.user.UserProxy;
import com.miqtech.master.wyrecorder.utils.TimeUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by arvin on 2016/10/28.
 */
@LayoutId(R.layout.activity_liveend)
public class LiveEndActivity extends BaseAppCompatActivity {
    @BindView(R.id.img_bg)
    ImageView imgBg;
    Bundle data;
    int mOrientation;
    @BindView(R.id.tv_long)
    TextView tvLong;
	@BindView(R.id.tv_people_num)
    TextView tvPeopleNum;
    @BindView(R.id.tv_bait)
    TextView tvBait;
	@BindView(R.id.tv_yuer_coin)
    TextView tvYuerCoin;
    long time;
    int bait;
    int peopleNum ;
    int yuerCoin;
    final int MSG_UPDATE_VIEW = 0X801;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_UPDATE_VIEW:
                    tvPeopleNum.setText(peopleNum + "");
                    tvBait.setText(bait+ "");
                    tvYuerCoin.setText(yuerCoin+ "");
                    break;
            }
        }
    };
    @Override
    protected void initViews(Bundle savedInstanceState) {
        data = getIntent().getExtras();
        mOrientation = data.getInt("screen");
        time = data.getLong("mills");
        tvLong.setText(TimeUtils.formatTime(time, "HH:mm:ss"));
        if (mOrientation == 0) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            imgBg.setImageResource(R.drawable.bg_endlive);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            imgBg.setImageResource(R.drawable.bg_endlive_landscape);
        }

        closeLive();
    }

    private void closeLive() {
        User user = UserProxy.getUser();
        if (user != null) {
            Map<String, String> params = new HashMap<>();
            params.put("token", user.getToken());
            params.put("userId", user.getId());
            sendHttpRequest(API.CLOSE_LIVE, params);
        }
    }
    @Override
    public void onSuccess(JSONObject object, String method) {
        super.onSuccess(object, method);
        try {
            switch (method) {
                case API.CLOSE_LIVE:
                    JSONObject jsonObject = object.getJSONObject("object");
                    bait = jsonObject.getInt("bait");
                    peopleNum = jsonObject.getInt("people_num");
                    yuerCoin = jsonObject.getInt("yuer_coin");
                    if (time < 1000 * 60) {
                        bait = 0;
                        peopleNum = 0;
                        yuerCoin = 0;
                    }
                    handler.sendEmptyMessage(MSG_UPDATE_VIEW);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.tv_back})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                finish();
                jumpToActivity(MainActivity.class);
                break;
        }
    }



}
